/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.ARTesting;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.Utils.LocationUtils;


public class TestingSamples extends Activity{
	private static final int MENU_SAVE = Menu.FIRST + 1;
	private static final int MENU_SET_REAL_POS = Menu.FIRST + 2;
	
	private static final String[] tag_modes = {"Immediate", "Fast", "Map", "Accurate_side", "Accurate_line"};
	
	private Spinner spinner;
	private EditText set_distance;
	private EditText set_height;
	private EditText set_name;
	private Button button;
	
	private String distance;
	private String height;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save_samples);
        
        spinner = (Spinner)findViewById(R.id.spinner);
        
        ArrayAdapter<CharSequence> adapter =  new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
        for(int i = 0; i<tag_modes.length; i++){
        	adapter.add(tag_modes[i]);
        }
        
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        TextView tv_dist = (TextView)findViewById(R.id.measured_distance);
        distance = getIntent().getExtras().getString("DISTANCE");
        tv_dist.setText("Measured distance: " + distance);
        
        TextView tv_height = (TextView)findViewById(R.id.measured_height);
        height = getIntent().getExtras().getString("HEIGHT");
        tv_height.setText("Measured height: " + height);
        
        set_distance = (EditText)findViewById(R.id.set_distance);
        set_height = (EditText)findViewById(R.id.set_height);
        set_name = (EditText)findViewById(R.id.set_name);
        
        button = (Button) findViewById(R.id.test_button);
        button.setOnClickListener(onClick);
	}
	
	OnClickListener onClick = new OnClickListener(){

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			try{
				float dist = Float.parseFloat(set_distance.getText().toString());
				float h = Float.parseFloat(set_height.getText().toString());
				
				float[] coords = {(float)TestingManager.getUserLocation().getLatitude(),
						(float)TestingManager.getUserLocation().getLongitude()};
				
				float[] res_coords = LocationUtils.distance2Coord(coords, TestingManager.getAzimuth(), dist);
				
				h += (float)TestingManager.getUserLocation().getAltitude();
				TestingManager.setResRealLoc(res_coords[0], res_coords[1], h);
				
				Toast.makeText(TestingSamples.this, "Ok", Toast.LENGTH_LONG);
			}catch(Exception e){
				Toast.makeText(TestingSamples.this, "Error", Toast.LENGTH_LONG);
			}
		}
		
	};
	
	private String writeSample(){
		
		return TestingManager.writeSample(tag_modes[spinner.getSelectedItemPosition()], set_name.getText().toString());
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	menu.add(0, MENU_SAVE, 0, "Save")
    			.setIcon(R.drawable.floppy_disk_48);
    	menu.add(0, MENU_SET_REAL_POS, 0, "Set real position (map)");
    	
        super.onCreateOptionsMenu(menu);        
        return true;
    }

    public boolean onOptionsItemSelected (MenuItem item) {
    	
    		
    	switch (item.getItemId()) {

    	case MENU_SAVE:
    		
    		Toast.makeText(this, writeSample(), Toast.LENGTH_LONG).show();
    		
    		break;
    		
    	case MENU_SET_REAL_POS:
    		Intent i = new Intent(this, RealLocation.class);
    		
    		startActivityForResult(i, 1);
    		break;
    		
    	}
        return super.onOptionsItemSelected(item);
    }
    
	protected void onActivityResult (int requestCode, int resultCode, Intent data) { 
		
		switch (requestCode) { 
		
    		
	    	case 1:
	    		
	    		if(TestingManager.getResRealLocation() == null)
	    			break;
	    		float dist = TestingManager.getResRealLocation().distanceTo(TestingManager.getUserLocation());
	    		set_distance.setText(Float.toString(dist));
	    		
	    		float h = (float) (TestingManager.getResRealLocation().getAltitude() - TestingManager.getUserLocation().getAltitude());
	    		set_height.setText(Float.toString(h));
	    		
	    		break;
	
            default:
                break; 
		}
	}
	
}